package com.example.liulili.gps;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.logging.Handler;

/**
 * http://www.cnblogs.com/mythou/p/3167648.html
 * http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
 *http://niravranpara.blogspot.jp/2013/04/android-get-current-gps-location.html
 *
 * http://blog.csdn.net/jimbo_lee/article/details/38511941
 */
public class MainActivity extends ActionBarActivity {

    private LocationManager locationManager;
    private Button button;
    private TextView jingdu;
    private TextView weidu;
    //private String provider;

    //更新监听位置信息变化
    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2 * 1000, 8, locationListener);
    }

    //当activity不处于活动状态时取消GPS监听
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(locationListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        button = (Button) findViewById(R.id.button);
        jingdu = (TextView) findViewById(R.id.textView2);
        weidu  = (TextView) findViewById(R.id.textView4);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        /*
        //设置一些标准
        Criteria criteria = new Criteria();
        //设置最大精度
        criteria.setAccuracy(Criteria.ACCURACY_HIGH);
        //不使用海拔
        criteria.setAltitudeRequired(false);
        //不使用方位
        criteria.setBearingRequired(false);
        //是否允许收费
        criteria.setCostAllowed(true);
        //电量要求
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        provider = locationManager.getBestProvider(criteria,true);
        */

        //Log.d("gps provider",provider);

        if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            Log.d("GPS 状态", "不可用");
            //去手机设置中开启GPS
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
            //openGPS(getApplicationContext());
            //turnGPSOn();
           // Settings.Secure.putString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "network,gps");
        }


        final Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        showLocation(location);

        //设置监听，自动更新的最小时间间隔2s，最小的位移变化8米
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2 * 1000, 8, locationListener);


    }

    //位置监听
    private final LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            showLocation(location);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d("GPS","status changes..");
        }

        //GPS 可用时
        @Override
        public void onProviderEnabled(String provider) {
            showLocation(locationManager.getLastKnownLocation(provider));
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.d("GPS","status 无法使用");
        }

    };

    //显示最新信息
    private void showLocation(Location location){
        if(location != null){
            jingdu.setText(String.valueOf(location.getLongitude()));
            weidu.setText(String.valueOf(location.getLatitude()));

        }else {
            Toast.makeText(getApplicationContext(),"未获取地理信息",Toast.LENGTH_SHORT);
        }
    }


    private void netLocation(){
        String locateByNet = LocationManager.NETWORK_PROVIDER;
        Location locationNet = locationManager.getLastKnownLocation(locateByNet);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
